package com.bawei.demo_shoppingcar.view;


import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bawei.demo_shoppingcar.R;
import com.bawei.demo_shoppingcar.application.ContentData;
import com.bawei.demo_shoppingcar.bean.TabLayoutBean;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class TabLayoutFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String TAG = "TabLayoutFragment";

    private String mParam1;
    private String mParam2;
    private TextView tv;
    private GridView gridView;


    public TabLayoutFragment() {
    }

    public TabLayoutFragment(String mParam1, String mParam2) {
        this.mParam1 = mParam1;
        this.mParam2 = mParam2;
    }


    public static TabLayoutFragment newInstance(String param1, String param2) {
        TabLayoutFragment fragment = new TabLayoutFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_tab_layout, container, false);
        initView(inflate);
        initData();
        return inflate;
    }

    private void getGridViewData(final List<TabLayoutBean.DataBean> data) {

        gridView.setAdapter(new BaseAdapter() {

            private TextView goods_des;
            private TextView goods_name;
            private TextView goods_shop_price;
            private TextView goods_market_price;
            private ImageView goods_image;


            @Override
            public int getCount() {
                return data.size();
            }

            @Override
            public Object getItem(int position) {
                return data.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder ;
                if (convertView == null){
                    viewHolder = new ViewHolder();
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_news_recyclerview,parent,false);
                    viewHolder.goods_des = (TextView) convertView.findViewById(R.id.goods_des);
                    viewHolder.goods_name = (TextView) convertView.findViewById(R.id.goods_name);
                    viewHolder.goods_shop_price = (TextView) convertView.findViewById(R.id.goods_shop_price);
                    viewHolder.goods_market_price = (TextView) convertView.findViewById(R.id.goods_market_price);
                    viewHolder.goods_market_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                    viewHolder.goods_image = (ImageView) convertView.findViewById(R.id.goods_image);
                    convertView.setTag(viewHolder);
                }else {
                   viewHolder = (ViewHolder) convertView.getTag();
                }
                viewHolder.goods_name.setText(data.get(position).getEfficacy());
                viewHolder.goods_des.setText(data.get(position).getGoods_name());
                viewHolder.goods_shop_price.setText(data.get(position).getShop_price()+"¥");
                viewHolder.goods_market_price.setText(data.get(position).getMarket_price()+"¥");
                ImageLoader.getInstance().displayImage(data.get(position).getGoods_img(),viewHolder.goods_image,ContentData.options);
                return convertView;
            }
        });

    }
    class ViewHolder {

        TextView goods_name,goods_des,goods_shop_price,goods_market_price;
        ImageView goods_image;

    }

    private void initData() {

        HashMap<String, String> hashMap = ContentData.hashMap;
        Iterator<String> iterator = hashMap.keySet().iterator();
        String url = "";
        while (iterator.hasNext()){
            String next = iterator.next();
            String test = hashMap.get(next);
            if (mParam1.equals(next)){
                url = test;
            }
        }

        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i(TAG, " onFailure() IOException="+ e);
        }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                Gson gson = new Gson();
                TabLayoutBean tabLayoutBean = gson.fromJson(string, TabLayoutBean.class);
                final List<TabLayoutBean.DataBean> data = tabLayoutBean.getData();
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        getGridViewData(data);
                    }
                });
            }
        });
    }

    private void initView(View inflate) {
        gridView = (GridView) inflate.findViewById(R.id.grid_view);
    }

}
